<?php
/*
 * UniTpl
 * Unified features for templating with PHP and jQuery
 * http://joaobbn.com.br/projects/uni-tpl
 *
 * Copyright 2013 João BBN (João Baptista Bueno Neto)
 * Released under the MIT license
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 * 
 */

?>
<html>
<head>
    <title>UniTpl - Test #1</title>
</head>
<body>

<button id="twi_f5">Load the same template (via jQuery)</button>
<div id="tweets">
<?php
// BEGIN # from php-yell: https://code.google.com/p/php-yell/
function get ($url, $success_callback = FALSE, $error_callback = FALSE) {
    $data_return = file_get_contents($url,FILE_TEXT);
    
    if (is_callable($success_callback))
        $success_callback($data_return);
    
    if (is_callable($error_callback))
        $error_callback($data_return);
}

function my_each ($array, $iterate_callback = FALSE) {
    if (!is_array($array))
        return FALSE;
    
    if (is_callable($iterate_callback) && is_array($array)) {
        do {
            $index = key($array);
            
            $iterate_callback($index, $array[$index]);
        } while ($next = next($array));
    }
}
// END #

// Based on CodeIgniter parser library : https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Parser.php
function _parse_single($key, $val, $string) {
    return str_replace('{'.$key.'}', (string) $val, $string);
}

// get the template html file
get('twitter.tpl.html',function ($template) {
    $query = 'jobs';
    
    // get the data
    get('http://search.twitter.com/search.json?callback=&q='.$query, function ($data) use ($template) {
        $json = json_decode($data);
        
        my_each($json->results, function ($i, $item) use ($template){
            // I know, this part is very ugly
            $r = _parse_single('from_user',$item->from_user,$template);
            $r = _parse_single('text',$item->text,$r);
            echo $r;
        });
    });
});
?>
</div>

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="jquery.nano.js"></script>

<script type="text/javascript">
var query = 'jobs';
var template = 'twitter.tpl.html'; // template URL


$('#twi_f5').click(function (){
    // get the template html file
    $.get(template, function(template) {
        // get the data
        $.getJSON('http://search.twitter.com/search.json?callback=?', {q: query}, function(data) { 
            target = '#tweets';
            
            $(target).html(''); // clean the target div
            
            // iterate parsing the items
            $.each(data.results, function(i, item){
                console.log(item);
                $(target).append($(this).nano(template, item));
            })
        });
    });
});
</script>

</body>
</html>
